package dk.flashnotes.fff.tree.parameters {
	import flash.events.EventDispatcher;	

	/**
	 * @author jk
	 */
	public class NodeParameterControl extends EventDispatcher {

		private var _o_oNPs : Object;

		public function NodeParameterControl() {
			_o_oNPs = new Object();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// public interface 
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function setParmameter(sKey : String, oValue : *, bDisplayInURL : Boolean=true, bDispatchChange : Boolean=true) : void {
			
			oValue = oValue.toString();
			
			if (oValue== "" || oValue==null) {
				clearParameter(sKey);
			} else {
				doSetParameter(sKey, oValue.toString(), bDisplayInURL, bDispatchChange);
			}
		}

		public function get o_oNPs() : Object {
			return _o_oNPs;		}
		public function get_sParameterValue(sKey : String, sValueToReturnIfUndefined : String = "") : String {
			var sValue : String = get_sValue(sKey);
			if (sValue) {
				return sValue;
			} else {
				return sValueToReturnIfUndefined;
			}
		}
		
		public function get_intParameterValue(sKey : String, intValueToReturnIfUndefined : int = 0) : int {
			var sValue : String = get_sValue(sKey);
			if (sValue) {
				return Number(sValue) as int;
			} else {
				return intValueToReturnIfUndefined;
			}
		}
		
		public function get_nParameterValue(sKey : String, intValueToReturnIfUndefined : Number = 0) : Number {
			var sValue : String = get_sValue(sKey);
			
			if (sValue!=null) {
				return Number(sValue) as int;
			} else {
				return intValueToReturnIfUndefined;
			}
		}		
		/**
		 * Clear a parameter from the given TreeNode
		 * 
		 * @param sKey 		String		The 'key' for the parameter to remove.	 
		 */		public function clearParameter(sKey : String) : void {
			delete _o_oNPs[sKey];
			dispatchEvent(new NodeParameterControlEvent(NodeParameterControlEvent.etCHANGED));
		}
		
		public function clearAllParameters() : void {
			_o_oNPs = new Object();
			dispatchEvent(new NodeParameterControlEvent(NodeParameterControlEvent.etCHANGED));
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// common setter
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function doSetParameter(sKey : String, sValue : *, bDisplayInURL : Boolean=true, bDispatchChange : Boolean=true) : void {
			var oNP : NodeParameter = get_oNPbyKey(sKey);
			oNP.bDisplayInURL = bDisplayInURL;
			oNP.sValue = sValue;
			if (bDispatchChange) {
				dispatchEvent(new NodeParameterControlEvent(NodeParameterControlEvent.etCHANGED));
			}
		}

		private function get_oNPbyKey(sKey : String) : NodeParameter {
			if(_o_oNPs[sKey] == null) {
				_o_oNPs[sKey] = new NodeParameter(sKey);
			}
			return _o_oNPs[sKey] as NodeParameter;		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// value gett
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function get_sValue(sKey : String) : String {
			
			var sExternalValue : String = get_sExternalParameter(sKey);
			
			if(_o_oNPs[sKey] != null) {
				return NodeParameter(_o_oNPs[sKey]).sValue;
			} else if (sExternalValue != null) {
				setParmameter(sKey, sExternalValue);
				return sExternalValue;
			} else {
				return null;
			}
		}

		private function get_sExternalParameter(sKey : String) : String {
			if (SWFAddress.getParameter(sKey)) {
				return unescape(SWFAddress.getParameter(sKey));
			}
			return null;
		}				public function lookForUpdate() : void {
			
			for each (var oParameter : NodeParameter in _o_oNPs) {
				
				if(oParameter.sValue!=get_sExternalParameter(oParameter.sKey)) {
					
					if (get_sExternalParameter(oParameter.sKey)==null) {
						oParameter.sValue=null;				
						dispatchEvent(new NodeParameterControlEvent(NodeParameterControlEvent.etCHANGED));
					} else {
						oParameter.sValue=get_sExternalParameter(oParameter.sKey);
						dispatchEvent(new NodeParameterControlEvent(NodeParameterControlEvent.etCHANGED));
					}
					
				}
			}
		}
		
		public function get a_oNPs() : Array {
			
			var aReturn : Array = new Array();
			for each (var oNP : NodeParameter in _o_oNPs) {
				aReturn.push(oNP);
			}
			return aReturn; 
		}
		
	}
}
